package com.xb.cloud.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.common.util.BeanUtils;
import com.xb.cloud.product.merchant.service.client.bo.ProductBo;
import com.xb.cloud.product.service.business.model.ADFiled;
import com.xb.cloud.product.service.business.model.ADFiledExample;
import com.xb.cloud.product.service.business.model.Product;
import com.xb.cloud.product.service.business.model.ProductDescription;
import com.xb.cloud.product.service.business.model.ProductDescriptionExample;
import com.xb.cloud.product.service.business.model.ProductExample;
import com.xb.cloud.product.service.business.model.ProductExample.Criteria;
import com.xb.cloud.product.service.business.model.ProductPic;
import com.xb.cloud.product.service.business.model.ProductPicExample;
import com.xb.cloud.product.service.business.model.ProductSerial;
import com.xb.cloud.product.service.business.model.ProductSerialExample;
import com.xb.cloud.product.service.business.model.PromotionCategory;
import com.xb.cloud.product.service.business.model.PromotionCategoryExample;
import com.xb.cloud.product.service.business.model.mapper.ADFiledMapper;
import com.xb.cloud.product.service.business.model.mapper.ProductDescriptionMapper;
import com.xb.cloud.product.service.business.model.mapper.ProductMapper;
import com.xb.cloud.product.service.business.model.mapper.ProductPicMapper;
import com.xb.cloud.product.service.business.model.mapper.ProductSerialMapper;
import com.xb.cloud.product.service.business.model.mapper.PromotionCategoryMapper;
import com.xb.cloud.product.service.client.ProductService;
import com.xb.cloud.product.service.client.SkuService;
import com.xb.cloud.product.service.client.bo.ADFiledBo;
import com.xb.cloud.product.service.client.bo.HomePageRecommendField;
import com.xb.cloud.product.service.client.bo.HomePageRecommendSku;
import com.xb.cloud.product.service.client.bo.ProductDescriptionBo;
import com.xb.cloud.service.client.ServiceReturnCodes;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

/**
 * 
 * @ClassName: ProductServiceImpl
 * @Description:TODO
 * @author: 鲜备科技
 * @date: 2017年12月20日 下午2:41:06
 * 
 * @Copyright: 2017 www.xianbeitech.com Inc. All rights reserved.
 *
 */
@Service
public class ProductServiceImpl implements ProductService {
	@Resource
	ProductMapper productMapper;

	@Resource
	ProductPicMapper productPicMapper;
	@Resource
	PromotionCategoryMapper promotionCategoryMapper;
	@Resource
	ADFiledMapper adFiledMapper;

	@Autowired
	SkuService skuServcie;
	
	@Resource
	ProductDescriptionMapper productDescriptionMapper;
	@Resource
	ProductSerialMapper productSerialMapper;

	/**
	 * 
	 * <p>
	 * Title: obtainH5HomeAD
	 * </p>
	 * <p>
	 * Description:获取首页广告位数据
	 * </p>
	 * 
	 * @param companyID
	 * @return
	 * @see com.xb.cloud.product.service.client.ProductService#getH5HomeADs(long)
	 */
	@Override
	public ServiceReturnInfo getH5HomeADs(long companyID) {
		List<ADFiledBo> adFiledBoList = new ArrayList<ADFiledBo>();
		//ServiceReturnInfo returnInfo = skuServcie.getRecommendSkuByComID(companyID);
		
		ADFiledExample example = new ADFiledExample();
		com.xb.cloud.product.service.business.model.ADFiledExample.Criteria criteria = example.createCriteria();
		criteria.andCompanyIdEqualTo(companyID);
		criteria.andIsEnabledEqualTo(DbConstants.PRODUCT_SKU_ENABLED);
		criteria.andIsDeletedEqualTo(DbConstants.IS_DELETED_FALSE);
		//H5 首页展示
		criteria.andDisplayChannelEqualTo((byte)0);
		List<ADFiled> filedList = adFiledMapper.selectByExample(example);
		
		if (filedList.isEmpty()) {
			
		} else {
			for (ADFiled adFiled : filedList) {
				ADFiledBo adFiledBo = new ADFiledBo();
				if(adFiled.getShowSort() != null) {
					adFiledBo.setIndex(adFiled.getShowSort());
				}
				if(adFiled.getType() != null) {
					adFiledBo.setType(adFiled.getType());
				}
				if(adFiled.getPicUrl() != null ) {
					
					adFiledBo.setPicURL(adFiled.getPicUrl());
				}
				if(adFiled.getJumpId() != null) {
					
					adFiledBo.setJumpId(adFiled.getJumpId());
				}
				if(adFiled.getJumpUrl() != null) {
					
					adFiledBo.setJumpUrl(adFiled.getJumpUrl());
				}
				
				adFiledBoList.add(adFiledBo);
			}

		}
		return ServiceReturnUtils.success(adFiledBoList);

	}

	/**
	 * 
	 * <p>
	 * Title: obtainH5RecommendFields
	 * </p>
	 * <p>
	 * Description: 获取推荐栏位数据
	 * </p>
	 * 
	 * @return
	 * @see com.xb.cloud.product.service.client.ProductService#obtainH5RecommendFields()
	 */
	@Override
	public List<HomePageRecommendField> getH5RecommendFields(long companyID) {
		List<HomePageRecommendField> fieldPOs = new ArrayList<HomePageRecommendField>();
		PromotionCategoryExample categoryExample = new PromotionCategoryExample();
		com.xb.cloud.product.service.business.model.PromotionCategoryExample.Criteria criteria = categoryExample
				.createCriteria();
		criteria.andCompanyIdEqualTo(companyID).andIsDeletedEqualTo((byte) 0);
		categoryExample.setOrderByClause("sort asc");
		List<PromotionCategory> list = promotionCategoryMapper.selectByExample(categoryExample);
		int index = 0;
		for (PromotionCategory promotionCategory : list) {
			HomePageRecommendField fieldPO = new HomePageRecommendField();
			fieldPO.setFieldName(promotionCategory.getName());
			fieldPO.setFieldIndex(index);
			index = index + 1;
			List<HomePageRecommendSku> promotionSkuByCategory = skuServcie
					.getPromotionSkuByCategory(promotionCategory.getId());
			fieldPO.setRecommendSKUList(promotionSkuByCategory);
			fieldPOs.add(fieldPO);
		}
		return fieldPOs;
	}

	/**
	 * 
	 * <p>
	 * Title: obtainSpecificationByID
	 * </p>
	 * <p>
	 * Description:获取产品规格
	 * </p>
	 * 
	 * @param productId
	 * @return
	 * @see com.xb.cloud.product.service.client.ProductService#getSpecificationByID(java.lang.Long)
	 */
	@Override
	public String getSpecificationByID(Long productId) {
		String specification = productMapper.selectSpecificationByID(productId);

		return specification;
	}
	/**
	 * 根据ProductID 获取product
	 * 
	 */
	@Override
	public ServiceReturnInfo getProductByID(Long productId,int type) {
		switch (type) {
		case 0:
			//获取product 无论该product是否可用，是否被删除
			Product product = productMapper.selectByPrimaryKey(productId);
			return ServiceReturnUtils.success(BeanUtils.newAndCopyProperties(ProductBo.class, product));
		case 1:
			//获取product，该product可用&没有被删除
			ProductExample example = new ProductExample();
			Criteria criteria = example.createCriteria();
			criteria.andIdEqualTo(productId);
			criteria.andIsEnabledEqualTo(DbConstants.PRODUCT_SKU_CAN_SALE);
			criteria.andIsDeletedEqualTo(DbConstants.IS_DELETED_FALSE);
			List<Product> productList = productMapper.selectByExample(example);
			if(productList.isEmpty()) {
				return ServiceReturnUtils.error(ServiceReturnCodes.PRODUCT_NOT_EXIST_EXCEPTION);
			}
			return ServiceReturnUtils.success(BeanUtils.newAndCopyProperties(ProductBo.class, productList.get(0)));

		default:
			return ServiceReturnUtils.error(ServiceReturnCodes.PRODUCT_NOT_EXIST_EXCEPTION);
		}
		
		
	}
	
	
	@Override
	public List<String> getProudctPicList(Long id) {
		ProductPicExample example = new ProductPicExample();
		com.xb.cloud.product.service.business.model.ProductPicExample.Criteria criteria = example.createCriteria();
		criteria.andProductIdEqualTo(id);
		example.setOrderByClause("pic_order asc");
		List<ProductPic> picBoList = productPicMapper.selectByExample(example);
		List<String> picList = new ArrayList<String>();
		for (ProductPic productPic : picBoList) {
			//0:默认图片
			if(productPic.getIsDefault() == 0) {
				picList.add(0, productPic.getPicUrl());
			} else {
				picList.add(productPic.getPicUrl());
			}
		}
		return picList;
	}
	/**
	 * 根据productID 获取product默认图片
	 */
	@Override
	public String getProductDefaultPicURL(Long productId) {
		ProductPicExample example = new ProductPicExample();
		com.xb.cloud.product.service.business.model.ProductPicExample.Criteria criteria = example.createCriteria();
		criteria.andProductIdEqualTo(productId);
		//默认图片
		criteria.andIsDefaultEqualTo((byte)1);
		List<ProductPic> picBoList = productPicMapper.selectByExample(example);
		if(picBoList.size() == 0) {
			return "";
		} else {
			return picBoList.get(0).getPicUrl();
		}
	}
	/**
	 * 根据productid 获取商品图文描述
	 */
	@Override
	public ServiceReturnInfo getProductDetailByID(Long productId) {
		ProductDescriptionExample descriptionExample = new ProductDescriptionExample();
		com.xb.cloud.product.service.business.model.ProductDescriptionExample.Criteria criteria = descriptionExample.createCriteria();
		criteria.andProductIdEqualTo(productId);
		List<ProductDescription> detailList = productDescriptionMapper.selectByExampleWithBLOBs(descriptionExample);
		if(detailList.isEmpty()) {
			return ServiceReturnUtils.error(ServiceReturnCodes.PRODUCT_DETAIL_NOT_EXIST_EXCEPTION);
		} else {
			ProductDescription detailDto = detailList.get(0);
			ProductDescriptionBo detailBo = new ProductDescriptionBo();
			detailBo.setDetailContent(detailDto.getDetailContent());
			return ServiceReturnUtils.success(detailBo);
		}
	}

	@Override
	public long getMianProductIdBySubProjectId(long subProjectId) {
		ProductSerialExample example = new ProductSerialExample();
		com.xb.cloud.product.service.business.model.ProductSerialExample.Criteria criteria = example.createCriteria();
		criteria.andSubProductIdEqualTo(subProjectId);
		
		List<ProductSerial> productSerialList = productSerialMapper.selectByExample(example);
		if(productSerialList.isEmpty()) {
			
			return -1;
		} else {
			return productSerialList.get(0).getProductId();
		}
	}

	

}
